package coolq.yaozhenyong.controller;

import java.util.List;

import com.forte.qqrobot.BaseConfiguration;
import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberIncrease;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberReduce;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.bean.GradeAdmin;
import coolq.yaozhenyong.bean.GradeUserGoto;
import coolq.yaozhenyong.custom.GroupHtmlAndQqService;
import coolq.yaozhenyong.custom.QqCqcustom;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimeUtils;
import coolq.yaozhenyong.custom.UpdateGroupList;
import coolq.yaozhenyong.service.GradeAdminService;
import coolq.yaozhenyong.service.GradeUserGotoService;
import coolq.yaozhenyong.service.GroupListService;

@Beans
public class QQGroupGotoController {
	
	public GradeAdminService gService = SpringContextUtil.getBean(GradeAdminService.class);
	public GroupListService grouplistservice = SpringContextUtil.getBean(GroupListService.class);
	/****
	 * 进群欢迎语
	 * 进群修改群名片
	 */
	@Listen(MsgGetTypes.groupMemberIncrease)
	@Filter	
	public void groupindex(GroupMemberIncrease msg,CQCodeUtil cqCodeUtil,MsgSender msgsender) {
			//1.取出改群的相关设置
			List<GradeAdmin> grade = gService.query().eq("gradenumber", msg.getGroup()).list();
			if(grade.size()>0) {
				GradeAdmin robot = new GradeAdmin();
				GradeAdmin gograde = new GradeAdmin();
				GradeAdmin gogradenikename = new GradeAdmin();
				for (GradeAdmin gradeAdmin : grade) {
					if(gradeAdmin.getCodes().equals("robot")&&gradeAdmin.getIsgoto()) {
						robot = gradeAdmin;
					}else if(gradeAdmin.getCodes().equals("gograde")&&gradeAdmin.getIsgoto()) {
						gograde = gradeAdmin;
					}else if(gradeAdmin.getCodes().equals("gogradenikename")&&gradeAdmin.getIsgoto()) {
						gogradenikename = gradeAdmin;
					}
				}
				//2. 判断机器人是否为启动状态 
				if(robot!=null) {
					if((robot.getIsgoto()==null)||(robot.getIsgoto()!=null&&robot.getIsgoto()==false)) {
						return ;
					}
					GradeUserGotoService gotoservice = SpringContextUtil.getBean(GradeUserGotoService.class);
					try {
						//3.判断是否需要修改群名片
						if(gogradenikename!=null) {
							if(gogradenikename.getIsgoto()!=null&&gogradenikename.getIsgoto()==true) {
								GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","nicknamegrade").one();
								if(GradeUserGoto!=null&&GradeUserGoto.getInputs()!=null&&GradeUserGoto.getInputs()!="") {
									msgsender.SETTER.setGroupCard(msg.getGroup(), msg.getBeOperatedQQ(), GroupHtmlAndQqService.mingpian(GradeUserGoto.getInputs(),cqCodeUtil,msg,msgsender));
									grouplistservice.update().eq("groupcode", msg.getGroup()).set("endtime", TimeUtils.getStringDate()).update();
								}
							}
						}
					} catch (Exception e) {
						msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[修改群名片]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
					}
					try {
						//4.判读是否需要进行进群欢迎
						if(gograde!=null&&gograde.getIsgoto()!=null&&gograde.getIsgoto()==true) {
							GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","gograde").one();
							if(GradeUserGoto!=null&&GradeUserGoto.getInputs()!=null&&GradeUserGoto.getInputs()!="") {
								msgsender.SENDER.sendGroupMsg(msg.getGroup(), GroupHtmlAndQqService.msggotograde(GradeUserGoto.getInputs(),cqCodeUtil,msg,msgsender));
								grouplistservice.update().eq("groupcode", msg.getGroup()).set("endtime", TimeUtils.getStringDate()).update();
							}
						}
					} catch (Exception e) {
						msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[用户加群欢送语]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
					}
				}
			}else {
				try {
					//BaseConfiguration.getLocalQQCode() 本机QQ号
					if(msg.getBeOperatedQQ().equals(BaseConfiguration.getLocalQQCode())) {
						UpdateGroupList.uodate(msgsender, cqCodeUtil);
						msgsender.SENDER.sendPrivateMsg("936642284",msg.getGroup()+"这是一个不受管理的新群，正在有人加入"+msg.getBeOperatedQQ());
						msgsender.SENDER.sendGroupMsg(msg.getGroup(), "大家好，我是熙酱！[CQ:face,id=205]\nPS：需要使用群管功能请将我设置为群管理！\n请本群管理添加群893778789,申请我的后台管理账号！（永久免费）");
						QqCqcustom.updateGroupList(msg,msgsender);
					}
				} catch (Exception e) {
					msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[更新群聊]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
				}
				
			}
		
	}
	/****
	 * 用户退群欢送语
	 * @param msg
	 * @param msgsender
	 */
	@Listen(MsgGetTypes.groupMemberReduce)
	@Filter
	public void to(GroupMemberReduce msg,MsgSender msgsender,CQCodeUtil cqCodeUtil) {
		try {
			GradeAdmin one = gService.query().eq("gradenumber", msg.getGroup()).eq("codes", "robot").eq("isgoto", "1").one();
			if(one!=null) {
				GradeAdmin one2 = gService.query().eq("gradenumber", msg.getGroup()).eq("codes", "tograde").eq("isgoto", "1").one();
				if(one2!=null) {
					GradeUserGotoService gotoservice = SpringContextUtil.getBean(GradeUserGotoService.class);
					GradeUserGoto GradeUserGoto = gotoservice.query().eq("qunnumber", msg.getGroup()).eq("type","tograde").one();
					if(GradeUserGoto!=null&&GradeUserGoto.getInputs()!=null&&GradeUserGoto.getInputs()!="") {
						msgsender.SENDER.sendGroupMsg(msg.getGroup(), GroupHtmlAndQqService.groupMemberReduce(GradeUserGoto.getInputs(),cqCodeUtil,msg,msgsender));
						grouplistservice.update().eq("groupcode", msg.getGroup()).set("endtime", TimeUtils.getStringDate()).update();
					}
				}
			}
		} catch (Exception e) {
			msgsender.SENDER.sendPrivateMsg("936642284","酷Q机器人发生错误，位置在[用户退群欢送语]!QQ群："+msg.getGroup()+"！时间在："+TimeUtils.getStringDate() );
		}
		
	}
}
